using System.Collections.Generic;
using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using UnityEngine;

namespace Game.Component
{
	public class ToothMachineDisplayerItem : BaseItem
	{
		private Transform game1;

		private Transform game2;

		private Transform game3;

		private int type;

		private List<BaseItem> toothTools = new List<BaseItem>();

		private Transform efc;

		private List<SpriteRenderer> game1Sprites = new List<SpriteRenderer>();

		private int game1Index;

		private float game1Progress;

		private List<SpriteRenderer> game2Sprites = new List<SpriteRenderer>();

		private Transform game2Sprite;

		private Transform game2CompleteSprite;

		private int game2Index;

		private float game2Progress;

		private List<SpriteRenderer> game3Sprites = new List<SpriteRenderer>();

		private Transform game3CompleteSprite;

		private int game3Index;

		private float game3Progress;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			Transform transform = model.Find("Displayer");
			game1 = transform.Find("Game1");
			game1.SetLocalZ(-1E-05f);
			game2 = transform.Find("Game2");
			game2.SetLocalZ(-1E-05f);
			game3 = transform.Find("Game3");
			game3.SetLocalZ(-1E-05f);
			int childCount = game1.childCount;
			for (int i = 0; i < childCount; i++)
			{
				Transform child = game1.GetChild(i);
				if (child.name.Contains("Hole"))
				{
					game1Sprites.Add(child.GetComponent<SpriteRenderer>());
				}
			}
			childCount = game2.childCount;
			for (int j = 0; j < childCount; j++)
			{
				Transform child2 = game2.GetChild(j);
				if (child2.name.Contains("Tile"))
				{
					game2Sprites.Add(child2.GetComponent<SpriteRenderer>());
				}
			}
			game2Sprite = game2.Find("Sprite");
			game2CompleteSprite = game2.Find("CompleteSprite");
			childCount = game3.childCount;
			for (int k = 0; k < childCount; k++)
			{
				Transform child3 = game3.GetChild(k);
				if (child3.name.Contains("Mask"))
				{
					game3Sprites.Add(child3.GetComponent<SpriteRenderer>());
				}
			}
			game3CompleteSprite = game3.Find("CompleteSprite");
		}

		protected override void LateUpdate()
		{
			if (type != 0)
			{
				AddProgress(Time.deltaTime);
			}
		}

		public void AddProgress(float val)
		{
			if (type != 0 && toothTools.Count > 0 && toothTools[0] != null)
			{
				if (type == 1)
				{
					AddGame1Progress(val);
				}
				else if (type == 2)
				{
					AddGame2Progress(val);
				}
				else if (type == 3)
				{
					AddGame3Progress(val);
				}
			}
		}

		public void AddToothTool(BaseItem item)
		{
			if (!toothTools.Contains(item))
			{
				toothTools.Add(item);
			}
			CheckDisplayerType();
		}

		public void RemoveToothTool(BaseItem item)
		{
			toothTools.Remove(item);
			CheckDisplayerType();
		}

		public void CheckDisplayerType()
		{
			if (toothTools.Count < 1)
			{
				SetDisplayerType(0);
				RemoveAudioPlayer();
			}
			else if (toothTools[0] != null)
			{
				if (toothTools[0].itemData.type == 12)
				{
					SetDisplayerType(1);
				}
				else if (toothTools[0].itemData.type == 13)
				{
					SetDisplayerType(2);
				}
				else if (toothTools[0].itemData.type == 14)
				{
					SetDisplayerType(3);
				}
				PlayAudio(toothTools[0].itemData.dragSound);
			}
		}

		private void PlayAudio(string[] sounds)
		{
			if (sounds != null)
			{
				Singleton<AudioManager>.Instance.PlayAudio(sounds[0], true, GetAudioPlayer());
			}
		}

		public void SetDisplayerType(int type)
		{
			if (this.type != type)
			{
				this.type = type;
				ResetEfc();
				game1.gameObject.SetActive(type == 1);
				game2.gameObject.SetActive(type == 2);
				game3.gameObject.SetActive(type == 3);
				switch (type)
				{
				case 1:
					ResetGame1();
					break;
				case 2:
					ResetGame2();
					break;
				case 3:
					ResetGame3();
					break;
				}
			}
		}

		private void ResetEfc()
		{
			if (efc != null)
			{
				Singleton<GamePoolManager>.Instance.Despawn(efc);
				efc = null;
			}
		}

		private void ResetGame1()
		{
			for (int i = 0; i < game1Sprites.Count; i++)
			{
				game1Sprites[i].gameObject.SetActive(false);
			}
			game1Index = 0;
			game1Progress = 0f;
		}

		private void AddGame1Progress(float val)
		{
			if (game1Index >= game1Sprites.Count)
			{
				return;
			}
			game1Progress += val / 2f;
			game1Sprites[game1Index].color = new Color(1f, 1f, 1f, game1Progress);
			game1Sprites[game1Index].gameObject.SetActive(true);
			if (game1Progress >= 1f)
			{
				game1Index++;
				game1Progress = 0f;
				if (game1Index >= game1Sprites.Count)
				{
					Singleton<AudioManager>.Instance.PlaySound("sound_blink");
					RemoveAudioPlayer();
					ResetEfc();
					efc = Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", game1.position + new Vector3(0f, 0f, -1E-05f), 3f);
				}
			}
		}

		private void ResetGame2()
		{
			for (int i = 0; i < game2Sprites.Count; i++)
			{
				game2Sprites[i].gameObject.SetActive(false);
			}
			game2Sprite.gameObject.SetActive(true);
			game2CompleteSprite.gameObject.SetActive(false);
			game2Index = 0;
			game2Progress = 0f;
		}

		private void AddGame2Progress(float val)
		{
			if (game2Index >= game2Sprites.Count)
			{
				return;
			}
			game2Progress += val * 0.75f;
			game2Sprites[game2Index].color = new Color(1f, 1f, 1f, game2Progress);
			game2Sprites[game2Index].gameObject.SetActive(true);
			if (!(game2Progress >= 1f))
			{
				return;
			}
			game2Index++;
			game2Progress = 0f;
			if (game2Index >= game2Sprites.Count)
			{
				for (int i = 0; i < game2Sprites.Count; i++)
				{
					game2Sprites[i].gameObject.SetActive(false);
				}
				game2Sprite.gameObject.SetActive(false);
				game2CompleteSprite.gameObject.SetActive(true);
				RemoveAudioPlayer();
				Singleton<AudioManager>.Instance.PlaySound("sound_blink");
				ResetEfc();
				efc = Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", game1.position + new Vector3(0f, 0f, -1E-05f), 3f);
			}
		}

		private void ResetGame3()
		{
			for (int i = 0; i < game3Sprites.Count; i++)
			{
				game3Sprites[i].gameObject.SetActive(true);
				game3Sprites[i].color = new Color(1f, 1f, 1f, 1f);
			}
			game3CompleteSprite.gameObject.SetActive(false);
			game3Index = 0;
			game3Progress = 0f;
		}

		private void AddGame3Progress(float val)
		{
			if (game3Index >= game3Sprites.Count)
			{
				return;
			}
			game3Progress += val * 0.75f;
			game3Sprites[game3Index].color = new Color(1f, 1f, 1f, 1f - game3Progress);
			if (!(game3Progress >= 1f))
			{
				return;
			}
			game3Index++;
			game3Progress = 0f;
			if (game3Index >= game3Sprites.Count)
			{
				for (int i = 0; i < game3Sprites.Count; i++)
				{
					game3Sprites[i].gameObject.SetActive(false);
				}
				game3CompleteSprite.gameObject.SetActive(true);
				RemoveAudioPlayer();
				Singleton<AudioManager>.Instance.PlaySound("sound_blink");
				ResetEfc();
				efc = Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", game1.position + new Vector3(0f, 0f, -1E-05f), 3f);
			}
		}
	}
}
